<template>
  <router-view />
</template>

<script setup lang="ts">
import { onMounted, onUnmounted } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '@/store/auth'
import emitter from '@/utils/emitter'
import { useDialog } from 'naive-ui'

const authStore = useAuthStore()
const router = useRouter()
const dialog = useDialog()

const handleUnauthorized = () => {
  dialog.warning({
    title: '登录提示',
    content: '登录已失效',
    positiveText: '去登录',
    negativeText: '取消',
    onPositiveClick: () => {
      authStore.clearAuthState()
      router.push('/login')
    }
  })
}

onMounted(() => {
  emitter.on('unauthorized', handleUnauthorized)
})

onUnmounted(() => {
  emitter.off('unauthorized', handleUnauthorized)
})
</script>
